package Testclass.vueTest;

import Dao.JdbcTools3;
import Dao.jdbcTools;
import Model.VueStu;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/vueStuup")
public class VueTestupd extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        resp.setContentType("application/json");
        String data2 = req.getParameter("data2");
        System.out.println("data2 = " + data2);
        JSONObject jsonObject = JSONObject.parseObject(data2.toString());
        Integer id = Integer.valueOf( jsonObject.getString("id"));
        String name = jsonObject.getString("name");
        System.out.println("name = " + name);
        Integer age = Integer.valueOf( jsonObject.getString("age"));
        String hobit = jsonObject.getString("hobit");
       VueStu vueStu = new VueStu();
        vueStu.setId(id);
        vueStu.setName(name);
        vueStu.setAge(age);
        vueStu.setHobit(hobit);
        int update1 = 0;
        try {
            update1 = JdbcTools3.update1("update vuestu set name=?,age=?,hobit=? where id=?",name,age,hobit,id);
            System.out.println("update1 = " + update1);
            String res = JSON.toJSONString(update1);
            System.out.println("后端json格式的字符串 = " + res);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PrintWriter writer = resp.getWriter();
        if (update1>0){
            System.out.println(update1);

            writer.write("success");
        }else {
            writer.write("error");
        }
        writer.flush();
        writer.close();
    }
}
